/*
 * 文件名：StsQaJobCoreServiceImpl.java
 * 版权：Copyright by www.yunshows.com
 * 描述：
 * 修改人：Awoke
 * 修改时间：2020年2月15日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package top.infopub.job.sts.service.impl;


import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.infopub.exception.ServiceException;
import top.infopub.job.cap.domain.LoanFinDomain;
import top.infopub.job.cap.service.LoanFinService;
import top.infopub.job.fia.domain.ProBudgetOfgroupDtlDomain;
import top.infopub.job.fia.service.ProBudgetOfgroupDtlService;
import top.infopub.job.fia.service.ProBudgetOfgroupService;
import top.infopub.job.fia.service.SuperBudgetOfgroupDtlService;
import top.infopub.job.fia.service.SuperBudgetOfgroupService;
import top.infopub.job.hr.service.OrgInfoService;
import top.infopub.job.mkt.service.PreProjectService;
import top.infopub.job.sts.service.OrgAreaQaService;
import top.infopub.job.sts.service.OrgProQaService;
import top.infopub.job.sts.service.OrgSuperQaService;
import top.infopub.job.sts.service.ProSelfCaeService;
import top.infopub.job.sts.service.StsQaJobCoreService1_2;
import top.infopub.job.util.ParserUtil;
import top.infopub.model.fia.ProBudgetOfgroup;
import top.infopub.model.fia.ProBudgetOfgroupDtl;
import top.infopub.model.fia.SuperBudgetOfgroup;
import top.infopub.model.fia.SuperBudgetOfgroupDtl;
import top.infopub.model.hr.OrgInfo;
import top.infopub.model.sts.OrgAreaQa;
import top.infopub.model.sts.OrgProQa;
import top.infopub.model.sts.OrgSuperQa;
import top.infopub.model.sts.ProSelfCae;
import top.infopub.tdp.util.BizParaUtil;

import com.google.common.collect.Maps;


/**
 * 经营质量分析报表 数据归集汇总核心service实现 为给job服务</br>
 * 自行承接建安额（即：合同额） 和 融资额
 *
 * @author Awoke
 * @version 2020年2月18日
 * @see StsQaJobCoreServiceImpl1_2
 * @since
 */
@Service
public class StsQaJobCoreServiceImpl1_2 implements StsQaJobCoreService1_2 {

    private static final Logger log = LoggerFactory.getLogger(StsQaJobCoreServiceImpl1_2.class);

    @Autowired
    private ProSelfCaeService proSelfCaeService;

    @Autowired
    private OrgInfoService orgInfoService;

    @Autowired
    private OrgSuperQaService orgSuperQaService;

    @Autowired
    private OrgAreaQaService orgAreaQaService;

    @Autowired
    private OrgProQaService orgProQaService;

    @Autowired
    private LoanFinService loanFinService;

    @Autowired
    private PreProjectService preProjectService;

    @Autowired
    private SuperBudgetOfgroupService superBudgetOfgroupService;

    @Autowired
    private SuperBudgetOfgroupDtlService superBudgetOfgroupDtlService;

    @Autowired
    private ProBudgetOfgroupService proBudgetOfgroupService;

    @Autowired
    private ProBudgetOfgroupDtlService proBudgetOfgroupDtlService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void stsQaOfCaeJob() {
        /*
         * 现在自行承接建安额 只有总部有</br>
         * 汇总 中全部项目的自行承接建安额，作为总部的已完成数据 </br>
         */
        // 新增- 总部公司经营质量 数据  （自行承接建安额 即：合同额）
        Map<String, Object> superOrgBudgetWhere = Maps.newHashMap();
        superOrgBudgetWhere.put("belongsYear", ParserUtil.getYearOfPrevMonth());
        superOrgBudgetWhere.put("processStatus", "9");
        // 【建投总部年度控股指标预算 fia_super_budget_ofgroup】{合同额}
        List<SuperBudgetOfgroup> superOrgBudgetList = superBudgetOfgroupService.selectByMapWhere(superOrgBudgetWhere);
        // 预算不存在
        if (superOrgBudgetList == null || superOrgBudgetList.isEmpty()) {
            throw new ServiceException("[总部]经营质量分析指标-合同额 数据归集时，建投总部预算不存在~");
        }
        SuperBudgetOfgroup var = superOrgBudgetList.get(0);// 取第一条

        OrgSuperQa superQaAdd = new OrgSuperQa();
        superQaAdd.setHrOrgSuperId(BizParaUtil.get("super_org_id"));// 总部公司ID
        superQaAdd.setAimType(1);// 指标类型 1=自行承接建安额（合同额）
        superQaAdd.setBelongsYear(ParserUtil.getYearOfPrevMonth());
        superQaAdd.setBelongsMonth(ParserUtil.getMonthOfPrevMonth());
        superQaAdd.setStsMonth(ParserUtil.prevMonth());// 上个月

        // 当月计划
        Map<String, Object> orgBudgetDetailWhere = Maps.newHashMap();
        orgBudgetDetailWhere.put("superBudgetId", var.getId());
        orgBudgetDetailWhere.put("months", ParserUtil.getMonthOfPrevMonth());
        SuperBudgetOfgroupDtl plan = superBudgetOfgroupDtlService.selectByMapWhere(
                orgBudgetDetailWhere).get(0);
        superQaAdd.setCurMonthPlan(plan.getCotFee());// 原始单位：元
        superQaAdd.setCurMonthPlanTts(ParserUtil.unit2Tts(plan.getCotFee()));
        superQaAdd.setCurMonthPlanHms(ParserUtil.unit2Hms(plan.getCotFee()));
        // 年度计划
        superQaAdd.setYearAllPlan(var.getCotFeeSum());// 原始单位：元
        superQaAdd.setYearAllPlanTts(ParserUtil.unit2Tts(var.getCotFeeSum()));
        superQaAdd.setYearAllPlanHms(ParserUtil.unit2Hms(var.getCotFeeSum()));

        // 当月实际完成
        Map<String, Object> proSelfCaeWhere = Maps.newHashMap();
        proSelfCaeWhere.put("noticeDateStart", ParserUtil.prevMonth());
        proSelfCaeWhere.put("noticeDateEnd", ParserUtil.currentMonth());
        // 汇总所有项目的自行承接建安额为 总部的实际总完成合同额
        List<ProSelfCae> proCaeList = proSelfCaeService.selectAllProQaOfCae(proSelfCaeWhere);
        if (proCaeList == null || proCaeList.isEmpty()) {
            // 实际当月完成为0
            superQaAdd.setCurMonthFinish(BigDecimal.ZERO);
            superQaAdd.setCurMonthFinishTts(BigDecimal.ZERO);
            superQaAdd.setCurMonthFinishHms(BigDecimal.ZERO);
        } else {
            // 实际当月完成 
            superQaAdd.setCurMonthFinish(proCaeList.get(0).getSubtotalCae());
            superQaAdd.setCurMonthFinishTts(proCaeList.get(0).getSubtotalCaeTts());
            superQaAdd.setCurMonthFinishHms(proCaeList.get(0).getSubtotalCaeHms());
        }
        if (superQaAdd.getCurMonthPlan().compareTo(BigDecimal.ZERO) == 0) {
            superQaAdd.setCurMonthRate(BigDecimal.ZERO);
        } else {
            superQaAdd.setCurMonthRate(superQaAdd.getCurMonthFinish().divide(
                    superQaAdd.getCurMonthPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                    ParserUtil.HUNDRED));// 完成率 =实际完成/计划
        }
        Map<String, Object> prevWhere = Maps.newHashMap();
        prevWhere.put("hrOrgSuperId", BizParaUtil.get("super_org_id"));// 总部公司ID
        prevWhere.put("aimType", 1);// 指标类型 1=自行承接建安额
        prevWhere.put("belongsYear", superQaAdd.getBelongsYear());
        prevWhere.put("belongsMonth", superQaAdd.getBelongsMonth() - 1);// 前一个月
        prevWhere.put("orderCondition", " add_time desc ");
        List<OrgSuperQa> prevList = orgSuperQaService.selectByMapWhere(prevWhere);

        // 月累数据   如果是当前年份的第一个月  或者 没有此前月累数据，则月累数据=当月数据  否则需要取前一个月的月累数据+当月数据 
        if (superQaAdd.getBelongsMonth() == 1 || prevList == null || prevList.isEmpty()) {
            superQaAdd.setYearAddupPlan(superQaAdd.getCurMonthPlan());
            superQaAdd.setYearAddupPlanTts(superQaAdd.getCurMonthPlanTts());
            superQaAdd.setYearAddupPlanHms(superQaAdd.getCurMonthPlanHms());
            superQaAdd.setYearAddupFinish(superQaAdd.getCurMonthFinish());
            superQaAdd.setYearAddupFinishTts(superQaAdd.getCurMonthFinishTts());
            superQaAdd.setYearAddupFinishHms(superQaAdd.getCurMonthFinishHms());
            superQaAdd.setYearAddupRate(superQaAdd.getCurMonthRate());
        } else {
            OrgSuperQa prevSuperQa = prevList.get(0);
            // 累加   当前月累 = 上月月累 + 当前
            superQaAdd.setYearAddupPlan(superQaAdd.getCurMonthPlan().add(
                    prevSuperQa.getYearAddupPlan()));
            superQaAdd.setYearAddupPlanTts(superQaAdd.getCurMonthPlanTts().add(
                    prevSuperQa.getYearAddupPlanTts()));
            superQaAdd.setYearAddupPlanHms(superQaAdd.getCurMonthPlanHms().add(
                    prevSuperQa.getYearAddupPlanHms()));
            superQaAdd.setYearAddupFinish(superQaAdd.getCurMonthFinish().add(
                    prevSuperQa.getYearAddupFinish()));
            superQaAdd.setYearAddupFinishTts(superQaAdd.getCurMonthFinishTts().add(
                    prevSuperQa.getYearAddupFinishTts()));
            superQaAdd.setYearAddupFinishHms(superQaAdd.getCurMonthFinishHms().add(
                    prevSuperQa.getYearAddupFinishHms()));
            // 计算月累完成率
            if (superQaAdd.getYearAddupPlan().compareTo(BigDecimal.ZERO) == 0) {
                superQaAdd.setYearAddupRate(BigDecimal.ZERO);
            } else {
                superQaAdd.setYearAddupRate(superQaAdd.getYearAddupFinish().divide(
                        superQaAdd.getYearAddupPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                        ParserUtil.HUNDRED));// 完成率 =实际完成/计划
            }
        }
        // 年度完成率  月累完成/年度计划 
        if (superQaAdd.getYearAllPlan().compareTo(BigDecimal.ZERO) == 0) {
            superQaAdd.setYearAllRate(BigDecimal.ZERO);
        } else {
            superQaAdd.setYearAllRate(superQaAdd.getYearAddupFinish().divide(
                    superQaAdd.getYearAllPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                    ParserUtil.HUNDRED));
        }
        superQaAdd.setAddBy("system");
        superQaAdd.setAddTime(new Date());
        superQaAdd.setUptBy("system");
        superQaAdd.setUptTime(new Date());
        orgSuperQaService.insert(superQaAdd);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void stsQaOfFinAmtJob() {
        /*
         * 建投总部/区域公司的融资额 预算值为包含的项目公司预算值合计
         */

        // 1. 新增- 项目公司经营质量 数据
        Map<String, Object> proBudgetWhere = Maps.newHashMap();
        proBudgetWhere.put("belongsYear", ParserUtil.getYearOfPrevMonth());
        proBudgetWhere.put("processStatus", "9");
        // 【项目指标预算fia_pro_budget_ofgroup】{融资额}
        List<ProBudgetOfgroup> proBudgetList = proBudgetOfgroupService.selectByMapWhere(proBudgetWhere);
        // 最外层的for循环 使用预算的org反查其实际完成 
        for (ProBudgetOfgroup var : proBudgetList) {
            OrgProQa proQaAdd = new OrgProQa();
            OrgInfo temp = new OrgInfo();
            temp.setProjectCode(var.getProCode());
            List<OrgInfo> orgs = orgInfoService.selectByEntityWhere(temp);
            // 如果公司项目没有绑定 则跳过
            if (orgs == null || orgs.isEmpty()) {
                log.info("项目：{} 与公司未绑定，统计时，skip~", var.getProCode());
                continue;
            } else {
                proQaAdd.setHrOrgAreaId(orgs.get(0).getParentId());
                proQaAdd.setHrOrgProId(orgs.get(0).getId());
                proQaAdd.setOrgProName(orgs.get(0).getCompName());
            }
            proQaAdd.setProCode(var.getProCode());
            proQaAdd.setProName(var.getProName());
            proQaAdd.setAimType(2);// 指标类型 2=融资额
            proQaAdd.setBelongsYear(ParserUtil.getYearOfPrevMonth());
            proQaAdd.setBelongsMonth(ParserUtil.getMonthOfPrevMonth());
            proQaAdd.setStsMonth(ParserUtil.prevMonth());// 上个月

            // 当月计划
            Map<String, Object> proBudgetDetailWhere = Maps.newHashMap();
            proBudgetDetailWhere.put("proBudgetId", var.getId());
            proBudgetDetailWhere.put("months", ParserUtil.getMonthOfPrevMonth());
            ProBudgetOfgroupDtl plan = proBudgetOfgroupDtlService.selectByMapWhere(
                    proBudgetDetailWhere).get(0);
            proQaAdd.setCurMonthPlan(plan.getFinFee());// 原始单位：元
            proQaAdd.setCurMonthPlanTts(ParserUtil.unit2Tts(plan.getFinFee()));
            proQaAdd.setCurMonthPlanHms(ParserUtil.unit2Hms(plan.getFinFee()));
            // 年度计划
            proQaAdd.setYearAllPlan(var.getFinSum());// 原始单位：元
            proQaAdd.setYearAllPlanTts(ParserUtil.unit2Tts(var.getFinSum()));
            proQaAdd.setYearAllPlanHms(ParserUtil.unit2Hms(var.getFinSum()));

            // 当月实际完成
            Map<String, Object> proQaWhere = Maps.newHashMap();
            proQaWhere.put("loanDateStart", ParserUtil.prevMonth());
            proQaWhere.put("loanDateEnd", ParserUtil.currentMonth());
            proQaWhere.put("proCode", var.getProCode());
            List<LoanFinDomain> loans = loanFinService.selectProQaOfFinAmt(proQaWhere);
            if (loans == null || loans.isEmpty()) {
                // 实际当月完成为0
                proQaAdd.setCurMonthFinish(BigDecimal.ZERO);
                proQaAdd.setCurMonthFinishTts(BigDecimal.ZERO);
                proQaAdd.setCurMonthFinishHms(BigDecimal.ZERO);
            } else {
                // 实际当月完成 
                BigDecimal curMonthAct = cpuActFinAmt(loans.get(0));
                proQaAdd.setCurMonthFinish(ParserUtil.tts2Unit(curMonthAct));
                proQaAdd.setCurMonthFinishTts(curMonthAct);
                proQaAdd.setCurMonthFinishHms(ParserUtil.tts2Hms(curMonthAct));
            }
            if (proQaAdd.getCurMonthPlan().compareTo(BigDecimal.ZERO) == 0) {
                proQaAdd.setCurMonthRate(BigDecimal.ZERO);
            } else {
                proQaAdd.setCurMonthRate(proQaAdd.getCurMonthFinish().divide(
                        proQaAdd.getCurMonthPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                        ParserUtil.HUNDRED));// 完成率 =实际完成/计划
            }

            Map<String, Object> prevWhere = Maps.newHashMap();
            prevWhere.put("hrOrgProId", proQaAdd.getHrOrgProId());
            prevWhere.put("aimType", 2);// 融资额
            prevWhere.put("belongsYear", proQaAdd.getBelongsYear());
            prevWhere.put("belongsMonth", proQaAdd.getBelongsMonth() - 1);// 前一个月
            prevWhere.put("orderCondition", " add_time desc ");
            List<OrgProQa> prevList = orgProQaService.selectByMapWhere(prevWhere);

            // 月累数据   如果是当前年份的第一个月  或者 没有此前月累数据，则月累数据=当月数据  否则需要取前一个月的月累数据+当月数据 
            if (proQaAdd.getBelongsMonth() == 1 || prevList == null || prevList.isEmpty()) {
                proQaAdd.setYearAddupPlan(proQaAdd.getCurMonthPlan());
                proQaAdd.setYearAddupPlanTts(proQaAdd.getCurMonthPlanTts());
                proQaAdd.setYearAddupPlanHms(proQaAdd.getCurMonthPlanHms());
                proQaAdd.setYearAddupFinish(proQaAdd.getCurMonthFinish());
                proQaAdd.setYearAddupFinishTts(proQaAdd.getCurMonthFinishTts());
                proQaAdd.setYearAddupFinishHms(proQaAdd.getCurMonthFinishHms());
                proQaAdd.setYearAddupRate(proQaAdd.getCurMonthRate());
            } else {
                OrgProQa prevProQa = prevList.get(0);
                // 累加   当前月累 = 上月月累 + 当前
                proQaAdd.setYearAddupPlan(proQaAdd.getCurMonthPlan().add(
                        prevProQa.getYearAddupPlan()));
                proQaAdd.setYearAddupPlanTts(proQaAdd.getCurMonthPlanTts().add(
                        prevProQa.getYearAddupPlanTts()));
                proQaAdd.setYearAddupPlanHms(proQaAdd.getCurMonthPlanHms().add(
                        prevProQa.getYearAddupPlanHms()));
                proQaAdd.setYearAddupFinish(proQaAdd.getCurMonthFinish().add(
                        prevProQa.getYearAddupFinish()));
                proQaAdd.setYearAddupFinishTts(proQaAdd.getCurMonthFinishTts().add(
                        prevProQa.getYearAddupFinishTts()));
                proQaAdd.setYearAddupFinishHms(proQaAdd.getCurMonthFinishHms().add(
                        prevProQa.getYearAddupFinishHms()));
                // 计算月累完成率
                if (proQaAdd.getYearAddupPlan().compareTo(BigDecimal.ZERO) == 0) {
                    proQaAdd.setYearAddupRate(BigDecimal.ZERO);
                } else {
                    proQaAdd.setYearAddupRate(proQaAdd.getYearAddupFinish().divide(
                            proQaAdd.getYearAddupPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                            ParserUtil.HUNDRED));// 完成率 =实际完成/计划
                }
            }
            // 年度完成率  月累完成/年度计划 
            if (proQaAdd.getYearAllPlan().compareTo(BigDecimal.ZERO) == 0) {
                proQaAdd.setYearAllRate(BigDecimal.ZERO);
            } else {
                proQaAdd.setYearAllRate(proQaAdd.getYearAddupFinish().divide(
                        proQaAdd.getYearAllPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                        ParserUtil.HUNDRED));
            }
            proQaAdd.setAddBy("system");
            proQaAdd.setAddTime(new Date());
            proQaAdd.setUptBy("system");
            proQaAdd.setUptTime(new Date());
            orgProQaService.insert(proQaAdd);
        }
        // 2. 新增- 区域公司经营质量 数据
        Map<String, Object> areaBudgetWhere = Maps.newHashMap();
        areaBudgetWhere.put("belongsYear", ParserUtil.getYearOfPrevMonth());
        areaBudgetWhere.put("months", ParserUtil.getMonthOfPrevMonth());
        List<ProBudgetOfgroupDtlDomain> areaMonthBudgetList = proBudgetOfgroupDtlService.sumMonthFinFeeOfArea(areaBudgetWhere);
        for (ProBudgetOfgroupDtlDomain var : areaMonthBudgetList) {
            if (StringUtils.isBlank(var.getHrOrgAreaId())) {
                log.info("所属区域公司ID为空，统计时，skip~");
                continue;
            }
            // 由于南充等项目 是挂到总部下面，按着区域分组会汇总到，但不能计入区域数据
            if (StringUtils.endsWithIgnoreCase(var.getHrOrgAreaId(),
                    BizParaUtil.get("super_org_id"))) {
                continue;
            }
            OrgAreaQa areaQaAdd = new OrgAreaQa();
            areaQaAdd.setHrOrgSuperId(BizParaUtil.get("super_org_id"));// 总部公司ID
            areaQaAdd.setHrOrgAreaId(var.getHrOrgAreaId());
            areaQaAdd.setOrgAreaName(orgInfoService.selectByPrimaryKey(var.getHrOrgAreaId()).getCompName());
            areaQaAdd.setAimType(2);// 指标类型 2=融资额
            areaQaAdd.setBelongsYear(ParserUtil.getYearOfPrevMonth());
            areaQaAdd.setBelongsMonth(ParserUtil.getMonthOfPrevMonth());
            areaQaAdd.setStsMonth(ParserUtil.prevMonth());// 上个月

            // 月度计划
            areaQaAdd.setCurMonthPlan(var.getFinFee());// 原始单位：元
            areaQaAdd.setCurMonthPlanTts(ParserUtil.unit2Tts(var.getFinFee()));
            areaQaAdd.setCurMonthPlanHms(ParserUtil.unit2Hms(var.getFinFee()));
            // 年度预算
            Map<String, Object> areaYearBudgetWhere = Maps.newHashMap();
            areaYearBudgetWhere.put("belongsYear", ParserUtil.getYearOfPrevMonth());
            areaYearBudgetWhere.put("hrOrgAreaId", var.getHrOrgAreaId());
            BigDecimal areaYearBudget = proBudgetOfgroupService.sumYearFinFeeOfArea(areaYearBudgetWhere);
            // 年度计划
            areaQaAdd.setYearAllPlan(areaYearBudget);// 原始单位：元
            areaQaAdd.setYearAllPlanTts(ParserUtil.unit2Tts(areaYearBudget));
            areaQaAdd.setYearAllPlanHms(ParserUtil.unit2Hms(areaYearBudget));

            // 当月实际完成
            Map<String, Object> qaWhere = Maps.newHashMap();
            qaWhere.put("loanDateStart", ParserUtil.prevMonth());
            qaWhere.put("loanDateEnd", ParserUtil.currentMonth());
            qaWhere.put("hrOrgAreaId", var.getHrOrgAreaId());
            List<LoanFinDomain> loans = loanFinService.selectAreaQaOfFinAmt(qaWhere);
            if (loans == null || loans.isEmpty()) {
                // 实际当月完成为0
                areaQaAdd.setCurMonthFinish(BigDecimal.ZERO);
                areaQaAdd.setCurMonthFinishTts(BigDecimal.ZERO);
                areaQaAdd.setCurMonthFinishHms(BigDecimal.ZERO);
            } else {
                // 实际当月完成 
                BigDecimal curMonthAct = cpuActFinAmt(loans.get(0));
                areaQaAdd.setCurMonthFinish(ParserUtil.tts2Unit(curMonthAct));
                areaQaAdd.setCurMonthFinishTts(curMonthAct);
                areaQaAdd.setCurMonthFinishHms(ParserUtil.tts2Hms(curMonthAct));
            }
            if (areaQaAdd.getCurMonthPlan().compareTo(BigDecimal.ZERO) == 0) {
                areaQaAdd.setCurMonthRate(BigDecimal.ZERO);
            } else {
                areaQaAdd.setCurMonthRate(areaQaAdd.getCurMonthFinish().divide(
                        areaQaAdd.getCurMonthPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                        ParserUtil.HUNDRED));// 完成率 =实际完成/计划
            }

            Map<String, Object> prevWhere = Maps.newHashMap();
            prevWhere.put("hrOrgAreaId", areaQaAdd.getHrOrgAreaId());
            prevWhere.put("aimType", 2);// 指标类型 2=融资额
            prevWhere.put("belongsYear", areaQaAdd.getBelongsYear());
            prevWhere.put("belongsMonth", areaQaAdd.getBelongsMonth() - 1);// 前一个月
            prevWhere.put("orderCondition", " add_time desc ");
            List<OrgAreaQa> prevList = orgAreaQaService.selectByMapWhere(prevWhere);

            // 月累数据   如果是当前年份的第一个月  或者 没有此前月累数据，则月累数据=当月数据  否则需要取前一个月的月累数据+当月数据 
            if (areaQaAdd.getBelongsMonth() == 1 || prevList == null || prevList.isEmpty()) {
                areaQaAdd.setYearAddupPlan(areaQaAdd.getCurMonthPlan());
                areaQaAdd.setYearAddupPlanTts(areaQaAdd.getCurMonthPlanTts());
                areaQaAdd.setYearAddupPlanHms(areaQaAdd.getCurMonthPlanHms());
                areaQaAdd.setYearAddupFinish(areaQaAdd.getCurMonthFinish());
                areaQaAdd.setYearAddupFinishTts(areaQaAdd.getCurMonthFinishTts());
                areaQaAdd.setYearAddupFinishHms(areaQaAdd.getCurMonthFinishHms());
                areaQaAdd.setYearAddupRate(areaQaAdd.getCurMonthRate());
            } else {
                OrgAreaQa prevAreaQa = prevList.get(0);
                // 累加   当前月累 = 上月月累 + 当前
                areaQaAdd.setYearAddupPlan(areaQaAdd.getCurMonthPlan().add(
                        prevAreaQa.getYearAddupPlan()));
                areaQaAdd.setYearAddupPlanTts(areaQaAdd.getCurMonthPlanTts().add(
                        prevAreaQa.getYearAddupPlanTts()));
                areaQaAdd.setYearAddupPlanHms(areaQaAdd.getCurMonthPlanHms().add(
                        prevAreaQa.getYearAddupPlanHms()));
                areaQaAdd.setYearAddupFinish(areaQaAdd.getCurMonthFinish().add(
                        prevAreaQa.getYearAddupFinish()));
                areaQaAdd.setYearAddupFinishTts(areaQaAdd.getCurMonthFinishTts().add(
                        prevAreaQa.getYearAddupFinishTts()));
                areaQaAdd.setYearAddupFinishHms(areaQaAdd.getCurMonthFinishHms().add(
                        prevAreaQa.getYearAddupFinishHms()));
                // 计算月累完成率
                if (areaQaAdd.getYearAddupPlan().compareTo(BigDecimal.ZERO) == 0) {
                    areaQaAdd.setYearAddupRate(BigDecimal.ZERO);
                } else {
                    areaQaAdd.setYearAddupRate(areaQaAdd.getYearAddupFinish().divide(
                            areaQaAdd.getYearAddupPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                            ParserUtil.HUNDRED));// 完成率 =实际完成/计划
                }
            }
            // 年度完成率  月累完成/年度计划 
            if (areaQaAdd.getYearAllPlan().compareTo(BigDecimal.ZERO) == 0) {
                areaQaAdd.setYearAllRate(BigDecimal.ZERO);
            } else {
                areaQaAdd.setYearAllRate(areaQaAdd.getYearAddupFinish().divide(
                        areaQaAdd.getYearAllPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                        ParserUtil.HUNDRED));
            }
            areaQaAdd.setAddBy("system");
            areaQaAdd.setAddTime(new Date());
            areaQaAdd.setUptBy("system");
            areaQaAdd.setUptTime(new Date());
            orgAreaQaService.insert(areaQaAdd);
        }
        // 3. 新增- 总部公司经营质量 数据
        // 月度预算
        Map<String, Object> superBudgetWhere = Maps.newHashMap();
        superBudgetWhere.put("belongsYear", ParserUtil.getYearOfPrevMonth());
        superBudgetWhere.put("months", ParserUtil.getMonthOfPrevMonth());
        BigDecimal superMonthBudget = proBudgetOfgroupDtlService.sumMonthFinFeeOfSuper(superBudgetWhere);
        if (superMonthBudget == null || superMonthBudget.compareTo(BigDecimal.ZERO) == 0) {
            throw new ServiceException("[总部]经营质量分析指标-融资额 数据归集时，公司预算不存在~");
        }
        // 年度预算
        Map<String, Object> superYearBudgetWhere = Maps.newHashMap();
        superYearBudgetWhere.put("belongsYear", ParserUtil.getYearOfPrevMonth());
        BigDecimal superYearBudget = proBudgetOfgroupService.sumYearFinFeeOfSuper(superYearBudgetWhere);
        if (superYearBudget == null || superYearBudget.compareTo(BigDecimal.ZERO) == 0) {
            throw new ServiceException("[总部]经营质量分析指标-融资额 数据归集时，公司预算不存在~");
        }

        OrgSuperQa superQaAdd = new OrgSuperQa();
        superQaAdd.setHrOrgSuperId(BizParaUtil.get("super_org_id"));// 总部公司ID
        superQaAdd.setAimType(2);// 指标类型 2=融资额
        superQaAdd.setBelongsYear(ParserUtil.getYearOfPrevMonth());
        superQaAdd.setBelongsMonth(ParserUtil.getMonthOfPrevMonth());
        superQaAdd.setStsMonth(ParserUtil.prevMonth());// 上个月
        // 月度计划
        superQaAdd.setCurMonthPlan(superMonthBudget);// 原始单位：元
        superQaAdd.setCurMonthPlanTts(ParserUtil.unit2Tts(superMonthBudget));
        superQaAdd.setCurMonthPlanHms(ParserUtil.unit2Hms(superMonthBudget));
        // 年度计划
        superQaAdd.setYearAllPlan(superYearBudget);// 原始单位：元
        superQaAdd.setYearAllPlanTts(ParserUtil.unit2Tts(superYearBudget));
        superQaAdd.setYearAllPlanHms(ParserUtil.unit2Hms(superYearBudget));

        // 当月实际完成
        Map<String, Object> qaWhere = Maps.newHashMap();
        qaWhere.put("loanDateStart", ParserUtil.prevMonth());
        qaWhere.put("loanDateEnd", ParserUtil.currentMonth());
        List<LoanFinDomain> loans = loanFinService.selectAllProQaOfFinAmt(qaWhere);
        if (loans == null || loans.isEmpty()) {
            // 实际当月完成为0
            superQaAdd.setCurMonthFinish(BigDecimal.ZERO);
            superQaAdd.setCurMonthFinishTts(BigDecimal.ZERO);
            superQaAdd.setCurMonthFinishHms(BigDecimal.ZERO);
        } else {
            LoanFinDomain var = loans.get(0);
            // 实际当月完成 
            BigDecimal curMonthAct = cpuActFinAmt(var);
            superQaAdd.setCurMonthFinish(ParserUtil.tts2Unit(curMonthAct));
            superQaAdd.setCurMonthFinishTts(curMonthAct);
            superQaAdd.setCurMonthFinishHms(ParserUtil.tts2Hms(curMonthAct));
        }
        if (superQaAdd.getCurMonthPlan().compareTo(BigDecimal.ZERO) == 0) {
            superQaAdd.setCurMonthRate(BigDecimal.ZERO);
        } else {
            superQaAdd.setCurMonthRate(superQaAdd.getCurMonthFinish().divide(
                    superQaAdd.getCurMonthPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                    ParserUtil.HUNDRED));// 完成率 =实际完成/计划
        }
        Map<String, Object> prevWhere = Maps.newHashMap();
        prevWhere.put("hrOrgSuperId", BizParaUtil.get("super_org_id"));// 总部公司ID
        prevWhere.put("aimType", 2);// 指标类型 2=融资额
        prevWhere.put("belongsYear", superQaAdd.getBelongsYear());
        prevWhere.put("belongsMonth", superQaAdd.getBelongsMonth() - 1);// 前一个月
        prevWhere.put("orderCondition", " add_time desc ");
        List<OrgSuperQa> prevList = orgSuperQaService.selectByMapWhere(prevWhere);

        // 月累数据   如果是当前年份的第一个月  或者 没有此前月累数据，则月累数据=当月数据  否则需要取前一个月的月累数据+当月数据 
        if (superQaAdd.getBelongsMonth() == 1 || prevList == null || prevList.isEmpty()) {
            superQaAdd.setYearAddupPlan(superQaAdd.getCurMonthPlan());
            superQaAdd.setYearAddupPlanTts(superQaAdd.getCurMonthPlanTts());
            superQaAdd.setYearAddupPlanHms(superQaAdd.getCurMonthPlanHms());
            superQaAdd.setYearAddupFinish(superQaAdd.getCurMonthFinish());
            superQaAdd.setYearAddupFinishTts(superQaAdd.getCurMonthFinishTts());
            superQaAdd.setYearAddupFinishHms(superQaAdd.getCurMonthFinishHms());
            superQaAdd.setYearAddupRate(superQaAdd.getCurMonthRate());
        } else {
            OrgSuperQa prevAreaQa = prevList.get(0);
            // 累加   当前月累 = 上月月累 + 当前
            superQaAdd.setYearAddupPlan(superQaAdd.getCurMonthPlan().add(
                    prevAreaQa.getYearAddupPlan()));
            superQaAdd.setYearAddupPlanTts(superQaAdd.getCurMonthPlanTts().add(
                    prevAreaQa.getYearAddupPlanTts()));
            superQaAdd.setYearAddupPlanHms(superQaAdd.getCurMonthPlanHms().add(
                    prevAreaQa.getYearAddupPlanHms()));
            superQaAdd.setYearAddupFinish(superQaAdd.getCurMonthFinish().add(
                    prevAreaQa.getYearAddupFinish()));
            superQaAdd.setYearAddupFinishTts(superQaAdd.getCurMonthFinishTts().add(
                    prevAreaQa.getYearAddupFinishTts()));
            superQaAdd.setYearAddupFinishHms(superQaAdd.getCurMonthFinishHms().add(
                    prevAreaQa.getYearAddupFinishHms()));
            // 计算月累完成率
            if (superQaAdd.getYearAddupPlan().compareTo(BigDecimal.ZERO) == 0) {
                superQaAdd.setYearAddupRate(BigDecimal.ZERO);
            } else {
                superQaAdd.setYearAddupRate(superQaAdd.getYearAddupFinish().divide(
                        superQaAdd.getYearAddupPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                        ParserUtil.HUNDRED));// 完成率 =实际完成/计划
            }
        }
        // 年度完成率  月累完成/年度计划 
        if (superQaAdd.getYearAllPlan().compareTo(BigDecimal.ZERO) == 0) {
            superQaAdd.setYearAllRate(BigDecimal.ZERO);
        } else {
            superQaAdd.setYearAllRate(superQaAdd.getYearAddupFinish().divide(
                    superQaAdd.getYearAllPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                    ParserUtil.HUNDRED));
        }
        superQaAdd.setAddBy("system");
        superQaAdd.setAddTime(new Date());
        superQaAdd.setUptBy("system");
        superQaAdd.setUptTime(new Date());
        orgSuperQaService.insert(superQaAdd);
    }

    /**
     * 指标发生金额说明  融资-【融资放款单】{融资金额}+融资-{融资放款单}{敞口金额}（*统计时间：放款日期，原始单位：万元）
     * @param var
     * @return
     * @see
     */
    private BigDecimal cpuActFinAmt(LoanFinDomain var) {
        return  null;
//        return var.getCapAmount().add(var.getCreditAmount());
    }

}
